# -*- coding: utf-8 -*-
"""
Created on Sun May  5 16:48:20 2019

@author: Administrator
"""
import numpy as np

# ndarray数组创建方法
# 1.从list/tuple等类型创建
# 使用list/tuple 或者[(), []]混合均可生成ndarray
# 如果不指定dtype时，自动根据数据情况关联一个类型

# ndarray = np.array(list/tuple) 
arr1 = np.array([1,2,3])
print(arr1) # [1 2 3]

arr2 = np.array((1,2,3))
print(arr2) # [1 2 3]

arr3 = np.array([(1,2,3),[1,2,3]])
print(arr3) # [[1 2 3], [1 2 3]]

# ndarray 常用创建方法
# 1.arange()/zeros()/ones()/full()/eye()：返回ndarray

# np.arange(n)
# 创建来自数值范围的数组:[0, n)
# 默认int32
default_arange = np.arange(10)
print(default_arange)

# (start, stop, step, dtype)
arange = np.arange(1, 25, 1, dtype = np.float)
print("arange:", arange)
print("arange ndim:", arange.ndim)

# np.empty(shape, dtype= float, order='C')
# dtype: 期望创建的数据类型，可选
# order: row-major array stype 'C' for C-stype, F for FORTRAN style
# 方法创建一个指定shape和dtype的未初始化的ndarray
empty = np.empty([3,2], dtype = int)
print(empty)
# note: 实际在测试时，发现已经被初始化为0

# np.zeros(shape, dtype=xx, order='C')
# 根据shape生成全0数组
# 默认为float64类型
zeros = np.zeros(5)
print(zeros)

zeros_int = np.zeros((3,1), dtype = np.int)
print(zeros_int)


# np.ones(shape, dtype= xx, order= 'C')
# 根据shape生成全1数组
# 默认为float64类型
ones = np.ones(5)
print(ones)

shaped_ones = np.ones([2,3], dtype = int)
print(shaped_ones)


# np.full(shape, val)
# 根据shape返回一个ndarray，元素值全为val
full = np.full([2,3], 5)
print(full)

# np.eye(n)
# 创建一个n*n二维单位数组
# 数据类型默认float64
eye = np.eye(4)
print(eye)


# 2.np.xx_like(a)
# 根据数组a的形状生成某种特征的ndarray
a = np.arange(4)
ones_a = np.ones_like(a)
print(ones_a) # [1 1 1 1]

zeros_a = np.zeros_like(a)
print(zeros_a)

full_a = np.full_like(a, 10)
print(full_a)


# 3. 数组其他创建
# np.concatenate(a, b)
concated = np.concatenate((a, zeros_a))
print(concated)

# numpy.asarray
# ndarray = numpy.asarray(input_data, dtype=xx, order=x)
# input_data 为任何形式的数据，如list，tuple等
# 默认不改变原数据类型

# list
int_list = [1,2,3]
int_ndarray = np.asarray(int_list)
print(int_ndarray)    # [1 2 3]

# list
float_list = [1.0, 2.0, 3.0]
float_ndarray = np.asarray(float_list)
print(float_ndarray)  # [ 1.  2.  3.]

# tuple
int_tuple = (1, 2, 3)
int_ndarray2 = np.asarray(int_tuple)
print(int_ndarray2)   # [1 2 3]

# tuple list
int_tuple_list = [(1,2,3), (3,4)]
tuple_list_ndarray = np.asarray(int_tuple_list)
print(tuple_list_ndarray)  # [(1, 2, 3) (3, 4)]
print(type(tuple_list_ndarray)) # np.savetxt()/loadtxt()




# 小结：
# ndarray的创建
# 使用list/tuple 或者[(), []]混合均可生成ndarray
# 常见创建方式：
# ndarray = np.array(list/tuple) 
# ndarray = np.arange()/zeros()/ones()/full()/eye()
# 数组其他创建
# ndarray = np.concatenate(a, b)